# 14. 最长公共前缀
# 编写一个函数来查找字符串数组中的最长公共前缀。
# 如果不存在公共前缀，返回空字符串 ""。
def longestCommonPrefix(strs):
    """
    :type strs: List[str]
    :rtype: str
    """
    loop_str = strs[0]
    result = []
    for i in range(len(loop_str)):
        char = loop_str[i]
        flag = True
        # 遍历每一个字符串指定下标
        for str_ in strs:
            # 此条件判断是否含有公共前缀
            if len(str_) <= i or str_[i] != char:
                flag = False
                break
        if flag:
            result.append(char)
        else:
            break
    return ''.join(result)


strs_1 = ["flower", "flow", "flight"]
result = longestCommonPrefix(strs_1)
print(f"result:{result}")

strs_2 = ["dog", "racecar", "car"]
result = longestCommonPrefix(strs_2)
print(f"result:{result}")

strs_3 = ["ab", "a" ]
result = longestCommonPrefix(strs_3)
print(f"result:{result}")